<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>营销管理</el-breadcrumb-item>
                <el-breadcrumb-item>位置管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="placeFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="placeFormData.pname" placeholder="请输入广告位置名称"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="select">查询</el-button>
                    <el-button type="primary" @click="insert">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="placeTableData"
                        :border="true"
                        stripe
                        style="width: 100%;font-size: 20px">
                    <el-table-column
                            prop="placeid"
                            label="编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="pname"
                            label="广告位置名称">
                    </el-table-column>

                    <el-table-column
                            prop="descrition"
                            label="说明">
                    </el-table-column>
                    <el-table-column
                            prop="type"
                            label="类型">
                    </el-table-column>

                    <el-table-column
                            prop="count"
                            label="广告容量">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="selectById(scope.row.placeid)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteById(scope.row.placeid)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!--分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>

            <div>
                <el-dialog
                        title="添加/编辑"
                        :visible.sync="customerPlaceForm"
                        width="30%">
                <span>
                    <el-form label-position="left" label-width="80px" :model="customerPlace" :rules="rules" ref="FromRef">
                          <el-form-item label="编号" v-if="show" >
                        <el-input v-model="customerPlace.placeid"></el-input>
                      </el-form-item>

                      <el-form-item label="广告位置名称" prop="pname">
                        <el-input v-model="customerPlace.pname"></el-input>
                      </el-form-item>

                      <el-form-item label="说明" prop="descrition">
                        <el-input  v-model="customerPlace.descrition"></el-input>
                      </el-form-item>
                        <el-form-item label="类型" prop="type">
                        <el-input v-model="customerPlace.type"></el-input>
                      </el-form-item>
                      <el-form-item label="广告容量" prop="count">
                        <el-input v-model.number="customerPlace.count"></el-input>
                      </el-form-item>
                    </el-form>
                </span>
                    <span slot="footer" class="dialog-footer">
                    <el-button @click="customerPlaceForm= false">取 消</el-button>
                    <el-button type="primary" @click="confirm">确 定</el-button>
                </span>
                </el-dialog>
            </div>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "CustomPlace",
        data(){
            return {
                //条件搜索对象
                placeFormData:{},

                //隐藏id
                show:false,

                rules:{
                    pname:[
                        { required: true, message: '请输入广告名称', trigger: 'blur' },
                    ],
                    descrition:[
                        { required: true, message: '填写说明', trigger: 'blur' },
                    ],
                    type:[
                        { required: true, message: '填写类型', trigger: 'blur' },
                    ],
                    count:[
                        { required: true, message: '填写广告容量', trigger: 'blur' }
                    ]
                },

                //表格数据
                placeTableData:[],
                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage:1,
                //总条数
                total:0,

                customerPlace: {},

                //表单数据
                customerPlaceForm:false,

            }
        },
        created() {
            //初始化菜单
            this.initPlaceTable()
        },
        methods:{
            //初始化用户列表
            initPlaceTable(){
                var that=this;
                this.$axios.post(`clms-market/market/place/initPlaceTable/${this.currentPage}/${this.pageSize}`,this.placeFormData).then(function (resp) {
                    that.placeTableData=resp.data.result.records;
                    that.total=resp.data.result.total;
                })
            },

            //添加和修改
            confirm(){
                var that=this;

                this.$refs.FromRef.validate((valid) => {
                    if (valid) {
                        if(this.customerPlace.placeid!=null){
                            this.$axios.put("clms-market/market/place/update",this.customerPlace).then(function (result) {
                                that.customerPlaceForm=false;
                                that.customerPlace={};
                                that.initPlaceTable();
                            })
                        }else {
                            this.$axios.post("clms-market/market/place/insert", this.customerPlace).then(function (result) {
                                that.customerPlaceForm = false;
                                that.initPlaceTable();
                            })
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },


            //条件查询按钮触发
            select(){
                this.initPlaceTable();
            },

            //添加按钮触发
            insert(){
                this.customerPlaceForm=true;
                this.customerPlace={};
            },


            //根据id查询回显
            selectById(id){
                var that=this;
                this.$axios.get(`clms-market/market/place/selectById/${id}`).then(function (returnResult) {
                    that.customerPlaceForm=true;
                    that.customerPlace=returnResult.data.result.huixian;
                    console.log(returnResult)
                })
            },

            //删除
            deleteById(id){
                var that=this;
                this.$confirm('确认删除此行?', '提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete("clms-market/market/place?id="+id).then(function (result)  {
                        that.initPlaceTable();

                    })
                })
            },

            //当改变每页显示的条数时触发该方法
            handleSizeChange(v){
                this.pageSize=v;
                this.initPlaceTable();

            },
            //切换页码时触发该方法
            handleCurrentChange(v){
                this.currentPage=v;
                this.initPlaceTable();
            },

        }
    }
</script>
<style scoped>
    .el-select .el-input {
        width: 130px;
    }
    .input-with-select .el-input-group__prepend {
        background-color: #fff;
    }
</style>